package brave;

import java.io.File;

import net.minecraft.server.WorldServer;

import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.entity.CraftPlayer;

public class PlayerListener implements Listener
{		
	@EventHandler
	public void onPlayerStart(PlayerJoinEvent event)
	{
		WallPlayer wallPlayer = Brave.getWallPlayer(event.getPlayer());
		wallPlayer.setState(WallPlayerState.DEFAULT);
		wallPlayer.getBukkitPlayer().teleport(Brave.origin_spawn_location);
	}
	
	@EventHandler
	public void onPlayerLogout(PlayerQuitEvent event)
	{
		// TODO: all this stuff is a big hack and needs to be fixed
		WallPlayer wallPlayer = Brave.getWallPlayer(event.getPlayer());
		if (wallPlayer.getState() == WallPlayerState.IN_GAME)
		{
			WallGame game = Brave.getWallGame(wallPlayer.getGameId());
			game.playerDied(wallPlayer);
		}
		else if (wallPlayer.getState() == WallPlayerState.OBSERVING_GAME)
		{
			WallGame game = Brave.getWallGame(wallPlayer.getGameId());
			if (game != null)
				game.removeObserver(wallPlayer);
		}
		
		wallPlayer.setState(WallPlayerState.DEFAULT);
		Location r = Brave.origin_spawn_location;
		((CraftPlayer)event.getPlayer()).getHandle().dimension=((CraftWorld)r.getWorld()).getHandle().dimension;
		((CraftPlayer)event.getPlayer()).getHandle().world = ((CraftWorld)r.getWorld()).getHandle();
		((CraftPlayer)event.getPlayer()).getHandle().locX=r.getX();
		((CraftPlayer)event.getPlayer()).getHandle().locY=r.getY();
		((CraftPlayer)event.getPlayer()).getHandle().locZ=r.getZ();
		
		Brave.removeWallPlayer(event.getPlayer());
	}
	
	@EventHandler
	public void onMoveEvent(PlayerMoveEvent event)
	{
		// quick exit if your high enough
		if (event.getTo().getY() > 132)
		{
			return;
		}
		
		WallPlayer wallPlayer = Brave.getWallPlayer(event.getPlayer());
		
		// if you hit the observing border bounce back up
		if (wallPlayer.getState() == WallPlayerState.OBSERVING_GAME &&
				event.getTo().getY() <= 132)
		{
			Player player = event.getPlayer();
			Location l = player.getLocation();
			player.sendMessage("~~~ spectator border ~~~");
			player.teleport(new Location(player.getWorld(), l.getX(), 135, l.getZ()));
			player.setFlying(true);
			event.setCancelled(true);
		}
	}
	
	@EventHandler
	public void onPlayerDealDamage(EntityDamageByEntityEvent event)
	{
		if (event.getEntityType() == EntityType.PLAYER)
		{
			WallPlayer wallPlayer = Brave.getWallPlayer(((Player)event.getEntity()));

			// if your not in game cancel
			if (wallPlayer.getState() != WallPlayerState.IN_GAME)
			{
				event.setCancelled(true);
			}
			
			// if your in game
			else
			{
				// and if your attacked by another player
				if (event.getDamager().getType() == EntityType.PLAYER)
				{
					// and if that player is not on your team
					WallPlayer attacker = Brave.getWallPlayer((Player)event.getDamager());
					if (attacker.getTeamId() == wallPlayer.getTeamId())
					{
						event.setCancelled(true);
					}
				}
			}
		}
	}
	
	@EventHandler
	public void onPlayerDie(EntityDeathEvent event)
	{
		if (event.getEntityType() == EntityType.PLAYER)
		{
			WallPlayer wallPlayer = Brave.getWallPlayer(((Player)event.getEntity()));

			if (wallPlayer.getState() == WallPlayerState.IN_GAME)
			{
				WallGame game = Brave.getWallGame(wallPlayer.getGameId());
				game.playerDied(wallPlayer);
			}
			
			if (wallPlayer.getState() == WallPlayerState.OBSERVING_GAME)
			{
				WallGame game = Brave.getWallGame(wallPlayer.getGameId());
				game.removeObserver(wallPlayer);
			}
			
			wallPlayer.setState(WallPlayerState.DEFAULT);
			wallPlayer.getBukkitPlayer().teleport(Brave.origin_spawn_location);
		}
	}
	
	@EventHandler
	public void onPlayerDropItem(PlayerDropItemEvent event)
	{
		if (Brave.getWallPlayer(event.getPlayer()).getState() == WallPlayerState.OBSERVING_GAME)
		{
			event.setCancelled(true);
		}
	}
}
